package com.bsbm.common.bean;

import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.model.TreeNodeImpl;

import javax.annotation.PostConstruct;
import javax.swing.tree.TreeNode;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ilya Shaikovsky
 *
 */
//@ManagedBean
//@ApplicationScoped
public class TreeBean extends TreeNodeImpl implements Serializable {
    //@ManagedProperty(value = "#{cdsParser.cdsList}")
    //private List<CDXmlDescriptor> cdXmlDescriptors;
    private List<CDXmlDescriptor> cdXmlDescriptors = new CDParser().getCdsList();

    private List<TreeNode> rootNodes = new ArrayList<TreeNode>();
    private Map<String, Country> countriesCache = new HashMap<String, Country>();
    private Map<String, Company> companiesCache = new HashMap<String, Company>();
    private TreeNode currentSelection = null;

    @PostConstruct
    public void init() {
        for (CDXmlDescriptor current : cdXmlDescriptors) {
            String countryName = current.getCountry();
            String companyName = current.getCompany();
            Country country = getCountryByName(current);
            Company company = getCompanyByName(current, country);
            CD cd = new CD(current.getTitle(), current.getArtist(), company, current.getPrice(), current.getYear());
            company.getCds().add(cd);
        }
    }

    public void processTreeToggle(TreeToggleEvent event){
        if(event.isExpanded()) {
            System.out.println("Inside processTreeToggle() for expanded event");
            UITree tree = (UITree) event.getSource();
            Object storedKey = tree.getRowKey();
            TreeNode selectedNode = (TreeNode) tree.getRowData();
            if (selectedNode instanceof Country) {
                Country country = (Country) selectedNode;
                System.out.println("No of companies under " + country.getName() + " is : " + country.getCompanies().size());
            } else if (selectedNode instanceof Company) {
                Company company = (Company) selectedNode;
                company.getCds();
                System.out.println("No of CDs under " + company.getName() + " is : " + company.getCds().size());
            }


            //UITreeNode node = (UITreeNode)event.getSource();
            //UITree tree1 = (UITree)node.getParent();
            // FileSystemNode selectedNode = tree1.getRowData();
            /*Iterator<Object> keys = selectedNode.getChildrenKeysIterator();
            for (FileSystemNode child : selectedNode.getDirectories()) {
                selectedNode.addChild(child.getFile().hashCode(), child);
            }  */
        }else {
                System.out.print("Inside processTreeToggle() for collapsed event");
        }
    }

    public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) {
        // considering only single selection
        List<Object> selection = new ArrayList<Object>(selectionChangeEvent.getNewSelection());
        Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) selectionChangeEvent.getSource();

        Object storedKey = tree.getRowKey();
        tree.setRowKey(currentSelectionKey);
        currentSelection = (TreeNode) tree.getRowData();
        tree.setRowKey(storedKey);
    }

    private Country getCountryByName(CDXmlDescriptor descriptor) {
        String countryName = descriptor.getCountry();
        Country country = countriesCache.get(countryName);
        if (country == null) {
            country = new Country();
            country.setName(countryName);
            countriesCache.put(countryName, country);
            rootNodes.add(country);
        }
        return country;
    }

    private Company getCompanyByName(CDXmlDescriptor descriptor, Country country) {
        String companyName = descriptor.getCompany();
        Company company = companiesCache.get(companyName);
        if (company == null) {
            company = new Company();
            company.setName(companyName);
            company.setParent(country);
            country.getCompanies().add(company);
            companiesCache.put(companyName, company);
        }
        return company;
    }

    public List<CDXmlDescriptor> getCdXmlDescriptors() {
        return cdXmlDescriptors;
    }

    public void setCdXmlDescriptors(List<CDXmlDescriptor> cdXmlDescriptors) {
        this.cdXmlDescriptors = cdXmlDescriptors;
    }

    public List<TreeNode> getRootNodes() {
        return rootNodes;
    }

    public void setRootNodes(List<TreeNode> rootNodes) {
        this.rootNodes = rootNodes;
    }

    public TreeNode getCurrentSelection() {
        return currentSelection;
    }

    public void setCurrentSelection(TreeNode currentSelection) {
        this.currentSelection = currentSelection;
    }

}